package com.alen.sun.service.Impl;

import org.apache.commons.codec.digest.DigestUtils;
import com.alen.sun.bean.Captcha;
import com.alen.sun.bean.Folk;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.CaptchaMapper;
import com.alen.sun.dao.FolkMapper;
import com.alen.sun.service.FolkService;
import com.alen.sun.util.PhoneCode;
import com.alen.sun.util.SmsSample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional
public class FolkServiceImpl implements FolkService {

    @Autowired
    private FolkMapper folkMapper;
    @Autowired
    private CaptchaMapper captchaMapper;

    /**
     * 添加家属
     * @param folk
     */
    @Override
    public Boolean addFolk(Folk folk) {
        if(folk.getPhoneNum()!=null && !folk.getPhoneNum().isEmpty()
                &&folk.getPassword()!=null && !folk.getPassword().isEmpty()
                &&folk.getName()!=null && !folk.getName().isEmpty()
                &&folk.getGender()!=null && !folk.getGender().isEmpty()) {
            //先判断该手机号是否已注册
            Integer count = folkMapper.selectFolk(folk);
            if(count == null || count == 0){
                //该手机号未注册
                folk.setElderlyId("000006");//设置默认状态的老人id,管理员获取这个老人id的家属账号,列表进行分配
                folk.setStatus(0); //设置默认账号状态为游客0,绑定老人后状态为1,购买套餐后状态为2
                //密码加密
                String encryptedPassword = DigestUtils.md5Hex(folk.getPassword());
                if (encryptedPassword != null) {
                    folk.setPassword(encryptedPassword);
                } else {
                    // 加密失败处理
                    return false;
                }
                folkMapper.addFolk(folk);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 注册家属账号
     * @param folk
     * @return
     */
    @Override
    public Boolean registerFolk(Folk folk,String captcha) {
        //先判断验证码是否正确
        Boolean flag = captchaMapper.selectByPhoneNumAndCaptcha(folk.getPhoneNum(), captcha);
        if(flag){
            //手机号和验证码正确
            if(folk.getPhoneNum()!=null && !folk.getPhoneNum().isEmpty()
                    &&folk.getPassword()!=null && !folk.getPassword().isEmpty()
                    &&folk.getName()!=null && !folk.getName().isEmpty()
                    &&folk.getGender()!=null && !folk.getGender().isEmpty()) {
                //先判断该手机号是否已注册
                Integer count = folkMapper.selectFolk(folk);
                if(count == null || count == 0){
                    //该手机号未注册
                    folk.setElderlyId("000006");//设置默认状态的老人id,管理员获取这个老人id的家属账号,列表进行分配
                    folk.setStatus(0); //设置默认账号状态为游客0,绑定老人后状态为1,购买套餐后状态为2
                    //密码加密
                    String encryptedPassword = DigestUtils.md5Hex(folk.getPassword());
                    if (encryptedPassword != null) {
                        folk.setPassword(encryptedPassword);
                    } else {
                        // 加密失败处理
                        return false;
                    }
                    folkMapper.addFolk(folk);
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }
        return false;
    }


    /**
     * 登录家属账号
     * @param folk
     * @return
     */
    @Override
    public Boolean login(Folk folk) {
        if(folk.getPhoneNum()!=null && !folk.getPhoneNum().isEmpty()
                &&folk.getPassword()!=null && !folk.getPassword().isEmpty()){
            //数据库查找记录
            Folk folk1 = folkMapper.selectPhoneNum(folk);
            if(folk1==null){
                log.info("不存在该用户");
                return false;
            }else if(DigestUtils.md5Hex(folk.getPassword()).equals(folk1.getPassword())){
                log.info("登录成功");
                return true;
            }else {
                log.info("账号或密码错误");
                return false;
            }
        }
        return false;

    }

    /**
     * 修改家属个人信息
     * @param folk
     */
    @Override
    public Boolean updateFolk(Folk folk) {
        if(folk.getPhoneNum()!=null && !folk.getPhoneNum().isEmpty()
            &&folk.getPassword()!=null && !folk.getPassword().isEmpty()
            &&folk.getName()!=null && !folk.getName().isEmpty()
            &&folk.getGender()!=null && !folk.getGender().isEmpty()
            &&folk.getStatus()!=null
            &&folk.getElderlyId()!=null && !folk.getElderlyId().isEmpty()){
            //先加密家属的密码
            String encryptedPassword = DigestUtils.md5Hex(folk.getPassword());
            if (encryptedPassword != null) {
                folk.setPassword(encryptedPassword);
            } else {
                // 加密失败处理
                return false;
            }
            Boolean flag = folkMapper.updateFolk(folk);
            return flag;
        }
        return false;
    }

    /**
     * 删除家属
     * @param folk
     */
    @Override
    public Boolean deleteFolk(Folk folk) {
        Boolean flag = folkMapper.deleteFolk(folk);
        return flag;
    }

    /**
     * 发送手机验证码
     * @param phoneNum
     */
    @Override
    public Boolean sendSms(String phoneNum) {
        if(phoneNum.matches("1[3-9]\\d{9}")){
            PhoneCode phoneCode = new PhoneCode();
            String captcha = phoneCode.generateCode();
            SmsSample smsSample = new SmsSample();
            smsSample.sendSms(phoneNum, captcha);

            Captcha captcha1 = new Captcha();
            captcha1.setPhoneNum(phoneNum);
            captcha1.setCaptcha(captcha);
            //将captcha保存到数据库--逻辑，先查询数据库是否有这个手机号，有->更新，无->添加
            if(captchaMapper.selectPhoneNum(phoneNum)!=null){
                //手机号存在
                captchaMapper.updateCaptcha(captcha1);
            }else {
                //手机号不存在
                captchaMapper.addCaptcha(captcha1);
            }
            return true;
        }
        return false;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Folk> folkList = folkMapper.page(start, pageSize);
        //2.查询总数
        Long count = folkMapper.count();
        PageBean pageBean = new PageBean(count,folkList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = folkMapper.deleteByIds(ids);
        return flag;
    }

    /**
     * 获取家属个人信息
     * @param phoneNum
     * @return
     */
    @Override
    public Folk getPersonalFolkInfo(String phoneNum) {
        Folk folk = folkMapper.getPersonalFolkInfo(phoneNum);
        return folk;
    }

    /**
     * 网页搜索功能,聚合模糊查询,姓名,电话
     * @param keywords
     * @return
     */
    @Override
    public List<Folk> searchByKeywords(String keywords) {
        List<Folk> folks = folkMapper.searchByKeywords(keywords);
        return folks;
    }


}
